package com.shwread.android.qysw.activity.psychological;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.ciko.code.views.MyListView;
import com.shwread.android.qysw.bean.que.OptionBean;
import com.shwread.android.qysw.bean.que.QueInfo;
import com.shwread.android.qysw10000242.R;

/**
 * 题目
 */
public class EvaluatingQuestionItemFragment extends Fragment implements EvaluatingOptionsListAdapter.UpListener {
    private static final String QUE_INDEX = "queIndex";
    private static final String QUE_INFO = "queInfo";
    /**
     * 题目标题
     */
    private TextView tvQueTitle;
    /**
     * 答题数/总数
     */
    private TextView tvQueCursor;
    /**
     * 填空
     */
    private EditText etQueTypeThree;
    /**
     * 上传凭证
     */
    private ImageView imgQueTypeFour;
    /**
     * 选项列表
     */
    private MyListView lvOptions;
    /**
     * 题目对象
     */
    private QueInfo queInfo;

    /**
     * 当前所在的题目位置
     */
    private int index;
    private EvaluatingOptionsListAdapter adapter;
    /**
     * 答案对象
     */
    private OptionBean queItemBean;

    public static EvaluatingQuestionItemFragment newInstance(int index, QueInfo dataInfo) {
        EvaluatingQuestionItemFragment fragment = new EvaluatingQuestionItemFragment();
        Bundle args = new Bundle();
        args.putInt(QUE_INDEX, index);
        args.putSerializable(QUE_INFO, dataInfo);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            index = bundle.getInt(QUE_INDEX);
            queInfo = (QueInfo) bundle.getSerializable(QUE_INFO);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View customView = inflater.inflate(R.layout.fragment_question, container, false);
        queItemBean = EvaluatingQuestionnaireDetailActivity.QueList.get(index);
        initView(customView);
        tvQueCursor.setText(Html.fromHtml("<big><big>" + (index + 1) + "</big></big>" + "/" + EvaluatingQuestionnaireDetailActivity.QueList.size()));
        itemIsShow();
        optionShow();

        lvOptions.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {//条目点击事件
                adapter.notifyDataSetChanged();
                for (int i = 0; i < queInfo.getQuestionnaireItemOptionInfos().size(); i++) {
                    if (lvOptions.isItemChecked(i)) {
                        QueInfo.QueOptionBean queOptionBean = queInfo.getQuestionnaireItemOptionInfos().get(i);
                        EvaluatingQuestionnaireDetailActivity.sourceList.put(queInfo.getId(), queOptionBean.getScore());
                        queItemBean.setOptionId(String.valueOf(queOptionBean.getId()));
                        queItemBean.setFinish(true);
                        return;
                    }
                }
                queItemBean.setOptionId("");//置空
                queItemBean.setFinish(false);
            }
        });
        return customView;
    }

    /**
     * 加载选项数据
     */
    private void optionShow() {
        tvQueTitle.setText(queInfo.getTitle());
        adapter = new EvaluatingOptionsListAdapter(queInfo.getQuestionnaireItemOptionInfos(), false, this);
        lvOptions.setAdapter(adapter);
        lvOptions.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

        String itemId = queItemBean.getOptionId();
        if (!TextUtils.isEmpty(itemId)) {
            for (int i = 0; i < queInfo.getQuestionnaireItemOptionInfos().size(); i++) {
                String id = String.valueOf(queInfo.getQuestionnaireItemOptionInfos().get(i).getId());
                if (itemId.equals(id)) {
                    lvOptions.setItemChecked(i, true);
                }
            }
        }
    }

    private void initView(View customView) {
        tvQueCursor = (TextView) customView.findViewById(R.id.tv_que_cursor);
        tvQueTitle = (TextView) customView.findViewById(R.id.tv_que_title);
        lvOptions = (MyListView) customView.findViewById(R.id.lv_options);
        etQueTypeThree = (EditText) customView.findViewById(R.id.et_que_type_three);
        imgQueTypeFour = (ImageView) customView.findViewById(R.id.img_que_type_four);
    }

    /**
     * 控制选项的显示和隐藏
     */
    void itemIsShow() {
        lvOptions.setVisibility(View.VISIBLE);
        etQueTypeThree.setVisibility(View.GONE);
        imgQueTypeFour.setVisibility(View.GONE);
    }

    @Override
    public void updateBackground(int position, View view) {
        if (lvOptions.isItemChecked(position)) {
            view.setBackgroundResource(R.drawable.item_ring_oval);
        } else {
            view.setBackgroundResource(R.drawable.item_ring);
        }
    }
}
